#!/usr/bin/python

import commonLib
from copy import deepcopy
import os
def findMaxDelta(NodeCollection):
	max = 0
	for node in NodeCollection:
		if node.delta > max:
			max = node.delta
	return max


def sumAllDeltas(NodeCollection):
	sum = 0
	for node in NodeCollection:
		sum += node.delta
	return sum

######## Functions used to generate a deltaTable according to a NodeCollection ######
def generateDeltaTable(NodeCollection):
	deltaTable = deepcopy(NodeCollection)
	adeltaTable = []
	#Now we just have to sort the Nodes depending on their deltas...
	for i in range(0, len(deltaTable)):
		index = indexOfMinDelta(deltaTable)
		adeltaTable.append(deepcopy(deltaTable[index]))
		deltaTable.remove(deltaTable[index])
	return adeltaTable

def indexOfMinDelta(deltaTable):
	index =-1
	min =9999999999
	for i, node in enumerate(deltaTable):
		if node.delta < min:
			min = node.delta
			index = i
	return index

################# End of generate deltaTable ###########

def saveStatus(ret,i,mySerialPortInterface):
	pwd = os.getcwd()
	f = open(pwd+'/../status'+str(mySerialPortInterface.port),'w')
	status = "Currently trying to optimize boot time. Progress: "+str(i)+"%. Last boot time: "+str(ret)+"."
	f.write(status)
	f.close()


def getIndexOfMinOf(array):
	index = 0
	min = 99999
	for i,value in enumerate(array):
		if value < min:
			min = value
			index =i
	return index
